﻿using Core;

namespace g_gongjianta
{
    public class SoundEvent : AbstractSkillEvent<TblSkill.SoundEvt>
    {
        private AudioObject audioObj;

        public override void Awake(long t1, TblSkill.Conf t2, TblSkill.SoundEvt t3)
        {
            base.Awake(t1, t2, t3);

            var sys = G.Get<AudioSystem>();
            audioObj = sys.Pop();
        }

        public override void Dispose()
        {
            if (IsDisposed) return;

            var sys = G.Get<AudioSystem>();
            sys?.Push(audioObj);
            audioObj = null;

            base.Dispose();
        }

        public override void Start()
        {
            base.Start();

            //log.err($"SoundEvt.Start id={EvtConf.EvtId}");

            audioObj.SetRes(EvtConf.Audio.ToString());
            audioObj.Time = EvtConf.Time;
            audioObj.Volume = EvtConf.Volume;
            //audioObj.volume = 1;
            audioObj.Start();
        }

        public override void Tick()
        {
            if (IsComplete) return;

            if (audioObj.IsComplete)
            {
                IsComplete = true;
            }
        }

        public override void Finish()
        {
            //log.err($"SoundEvt.Finish id={EvtConf.EvtId}");
            audioObj.Stop();
        }

        public override void Interrupt()
        {
            //log.err($"SoundEvt.Interrupt id={EvtConf.EvtId}");
        }
    }
}
